package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcPatientEntity;
import com.dhcc.cdc.busi.service.ICdcPatientService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 患者主信息
 * 
 * @ClassName: CdcPatientController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcpatient")
public class CdcPatientController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcpatient/";

	@Autowired
	private ICdcPatientService cdcPatientService;
	

    /**
     * 跳转到患者主信息列表首页
     */
    @RequestMapping("")
    @Permission("cdcpatient:list")
    public String index() {
        return PREFIX + "cdcpatient.html";
    }
	
    /**
     * 跳转到新增患者主信息
     */
    @RequestMapping("/to_add")
    @Permission("cdcpatient:add")
    public String toAdd() {
        return PREFIX + "cdcpatient_add.html";
    }
	
	/**
     * 跳转到修改患者主信息
     */
    @RequestMapping("/to_update/{cdcPatientId}")
    @Permission("cdcpatient:update")
    public String toUpdate(@PathVariable Long cdcPatientId, Model model) {
        CdcPatientEntity cdcPatient = this.cdcPatientService.selectById(cdcPatientId);
        model.addAttribute("cdcPatient",cdcPatient);
        return PREFIX + "cdcpatient_edit.html";
    }

    /**
     * 获取患者主信息列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcpatient:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcPatientEntity> page = new PageFactory<CdcPatientEntity>().defaultPage();
        List<CdcPatientEntity> result = this.cdcPatientService.page(page,params);
        page.setRecords((List<CdcPatientEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    @RequestMapping(value = "/getByCode/{patientCode}")
    @ResponseBody
    @Permission("cdcpatient:list")
    public Object getByCode(@PathVariable String patientCode) {
        CdcPatientEntity result = this.cdcPatientService.getByCode(patientCode);
        return result;
    }

    /**
     * 新增患者主信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcpatient:add")
    @BussinessLog(value = "新增患者主信息")
    public Object add(CdcPatientEntity cdcPatient) {
        if (CommonUtil.isOneEmpty(cdcPatient)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcPatient.insert();
        return R.ok();
    }

    /**
     * 删除患者主信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcpatient:delete")
    @BussinessLog(value = "删除患者主信息")
    public Object delete(@RequestParam Long cdcPatientId) {
	
        this.cdcPatientService.deleteById(cdcPatientId);

        return R.ok();
    }

    /**
     * 修改患者主信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcpatient:update")
    @BussinessLog(value = "修改患者主信息")
    public Object update(CdcPatientEntity cdcPatient) {
        if (CommonUtil.isOneEmpty(cdcPatient)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcPatientEntity old = this.cdcPatientService.selectById(cdcPatient.getId());

        old.setPatientCode(cdcPatient.getPatientCode());
        old.setHealthCode(cdcPatient.getHealthCode());
        old.setName(cdcPatient.getName());
        old.setGender(cdcPatient.getGender());
        old.setNationality(cdcPatient.getNationality());
        old.setNation(cdcPatient.getNation());
        old.setBirthdate(cdcPatient.getBirthdate());
        old.setAge(cdcPatient.getAge());
        old.setAgeMonth(cdcPatient.getAgeMonth());
        old.setAgeDay(cdcPatient.getAgeDay());
        old.setCardType(cdcPatient.getCardType());
        old.setCardValue(cdcPatient.getCardValue());
        old.setNativePlace(cdcPatient.getNativePlace());
        old.setNativeAddress(cdcPatient.getNativeAddress());
        old.setNowAddress(cdcPatient.getNowAddress());
        old.setCompany(cdcPatient.getCompany());
        old.setTelephone(cdcPatient.getTelephone());
        old.setEmail(cdcPatient.getEmail());
        old.setContactor(cdcPatient.getContactor());
        old.setContactorPhone(cdcPatient.getContactorPhone());
        old.setCareer(cdcPatient.getCareer());
        old.setBloodAbo(cdcPatient.getBloodAbo());
        old.setBloodRh(cdcPatient.getBloodRh());
        old.setDataId(cdcPatient.getDataId());
        old.setIsActive(cdcPatient.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
